// 社交活动
package auth

import (
	"github.com/gin-gonic/gin"
	v1 "last/app/controllers/v1"
	adminModel "last/app/models/admin"
	"last/app/requests"
	adminRequest "last/app/requests/admin"
	"last/pkg/respose"
	"strconv"
)

type SocialController struct {
	v1.BaseController
}

// Store 新增
func (s SocialController) Store(c *gin.Context) {
	var params adminRequest.SocialParams
	err := requests.Validator(&params, c)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	var SocialModel = adminModel.Socials{
		Title: params.Title,
		Href:  params.Href,
		Icon:  params.Icon,
	}
	result := SocialModel.Stroe(&SocialModel)
	if !result {
		respose.Failed(c, "新增失败")
		return
	}
	respose.Success(c, "", "创建成功")
}

// List 列表
func (s *SocialController) List(c *gin.Context) {
	var socialModel = adminModel.Socials{}
	page, _ := strconv.Atoi(c.Query("page"))
	pageSize, _ := strconv.Atoi(c.Query("page_size"))
	where := make(map[string]interface{}, 1)
	where["title"] = c.DefaultQuery("title", "")
	err, total, list := socialModel.List(page, pageSize, where)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	results := make(map[string]interface{})
	results["total"] = total
	results["list"] = list
	respose.Success(c, results, "获取成功")
}

// Show 详情
func (s *SocialController) Show(c *gin.Context) {
	id := c.Param("id")
	var socialModel = adminModel.Socials{}
	err := socialModel.Show(id, &socialModel)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	respose.Success(c, socialModel, "获取成功")
}

// Edit 更新
func (s *SocialController) Edit(c *gin.Context) {
	var params adminRequest.SocialParams
	err := requests.Validator(&params, c)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	var socialModel = adminModel.Socials{
		Title: params.Title,
		Icon:  params.Icon,
		Href:  params.Href,
	}
	result := socialModel.Edit(params.Id, &socialModel)
	if result != nil {
		respose.Failed(c, "更新失败")
		return
	}
	respose.Success(c, "", "更新成功")
}

// Remove 删除
func (s *SocialController) Remove(c *gin.Context) {
	id := c.Param("id")
	var socialModel = adminModel.Socials{}
	err := socialModel.Remove(id, socialModel)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	respose.Success(c, "", "删除成功")
}
